using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Csharp01._1118;

namespace Csharp01.TuShuGuanLi
{
    public partial class BookAddForm : Form
    {
        public BookAddForm()
        {
            InitializeComponent();
            LoadPublishers();
            LoadShelves();
        }

        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.txtISBN = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtBookName = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtAuthor = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.cboPublisher = new System.Windows.Forms.ComboBox();
            this.label5 = new System.Windows.Forms.Label();
            this.dtpPublishDate = new System.Windows.Forms.DateTimePicker();
            this.label6 = new System.Windows.Forms.Label();
            this.txtCategory = new System.Windows.Forms.TextBox();
            this.label7 = new System.Windows.Forms.Label();
            this.txtPrice = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.txtPages = new System.Windows.Forms.TextBox();
            this.label9 = new System.Windows.Forms.Label();
            this.txtDescription = new System.Windows.Forms.TextBox();
            this.label10 = new System.Windows.Forms.Label();
            this.cboShelf = new System.Windows.Forms.ComboBox();
            this.label11 = new System.Windows.Forms.Label();
            this.dtpPurchaseDate = new System.Windows.Forms.DateTimePicker();
            this.label12 = new System.Windows.Forms.Label();
            this.txtVersion = new System.Windows.Forms.TextBox();
            this.btnAdd = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(20, 20);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "ISBN:";
            // 
            // txtISBN
            // 
            this.txtISBN.Location = new System.Drawing.Point(100, 17);
            this.txtISBN.Name = "txtISBN";
            this.txtISBN.Size = new System.Drawing.Size(200, 21);
            this.txtISBN.TabIndex = 1;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(20, 50);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(59, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "图书名称:";
            // 
            // txtBookName
            // 
            this.txtBookName.Location = new System.Drawing.Point(100, 47);
            this.txtBookName.Name = "txtBookName";
            this.txtBookName.Size = new System.Drawing.Size(200, 21);
            this.txtBookName.TabIndex = 3;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(20, 80);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(35, 12);
            this.label3.TabIndex = 4;
            this.label3.Text = "作者:";
            // 
            // txtAuthor
            // 
            this.txtAuthor.Location = new System.Drawing.Point(100, 77);
            this.txtAuthor.Name = "txtAuthor";
            this.txtAuthor.Size = new System.Drawing.Size(200, 21);
            this.txtAuthor.TabIndex = 5;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(20, 110);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(47, 12);
            this.label4.TabIndex = 6;
            this.label4.Text = "出版社:";
            // 
            // cboPublisher
            // 
            this.cboPublisher.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboPublisher.FormattingEnabled = true;
            this.cboPublisher.Location = new System.Drawing.Point(100, 107);
            this.cboPublisher.Name = "cboPublisher";
            this.cboPublisher.Size = new System.Drawing.Size(200, 20);
            this.cboPublisher.TabIndex = 7;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(20, 140);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(59, 12);
            this.label5.TabIndex = 8;
            this.label5.Text = "出版日期:";
            // 
            // dtpPublishDate
            // 
            this.dtpPublishDate.Location = new System.Drawing.Point(100, 137);
            this.dtpPublishDate.Name = "dtpPublishDate";
            this.dtpPublishDate.Size = new System.Drawing.Size(200, 21);
            this.dtpPublishDate.TabIndex = 9;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(20, 170);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(59, 12);
            this.label6.TabIndex = 10;
            this.label6.Text = "图书分类:";
            // 
            // txtCategory
            // 
            this.txtCategory.Location = new System.Drawing.Point(100, 167);
            this.txtCategory.Name = "txtCategory";
            this.txtCategory.Size = new System.Drawing.Size(200, 21);
            this.txtCategory.TabIndex = 11;
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(20, 200);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(35, 12);
            this.label7.TabIndex = 12;
            this.label7.Text = "价格:";
            // 
            // txtPrice
            // 
            this.txtPrice.Location = new System.Drawing.Point(100, 197);
            this.txtPrice.Name = "txtPrice";
            this.txtPrice.Size = new System.Drawing.Size(200, 21);
            this.txtPrice.TabIndex = 13;
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(20, 230);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(35, 12);
            this.label8.TabIndex = 14;
            this.label8.Text = "页数:";
            // 
            // txtPages
            // 
            this.txtPages.Location = new System.Drawing.Point(100, 227);
            this.txtPages.Name = "txtPages";
            this.txtPages.Size = new System.Drawing.Size(200, 21);
            this.txtPages.TabIndex = 15;
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(20, 260);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(59, 12);
            this.label9.TabIndex = 16;
            this.label9.Text = "图书简介:";
            // 
            // txtDescription
            // 
            this.txtDescription.Location = new System.Drawing.Point(100, 257);
            this.txtDescription.Multiline = true;
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtDescription.Size = new System.Drawing.Size(200, 60);
            this.txtDescription.TabIndex = 17;
            // 
            // label10
            // 
            this.label10.AutoSize = true;
            this.label10.Location = new System.Drawing.Point(350, 20);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(35, 12);
            this.label10.TabIndex = 18;
            this.label10.Text = "书架:";
            // 
            // cboShelf
            // 
            this.cboShelf.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboShelf.FormattingEnabled = true;
            this.cboShelf.Location = new System.Drawing.Point(430, 17);
            this.cboShelf.Name = "cboShelf";
            this.cboShelf.Size = new System.Drawing.Size(200, 20);
            this.cboShelf.TabIndex = 19;
            // 
            // label11
            // 
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(350, 50);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(59, 12);
            this.label11.TabIndex = 20;
            this.label11.Text = "采购日期:";
            // 
            // dtpPurchaseDate
            // 
            this.dtpPurchaseDate.Location = new System.Drawing.Point(430, 47);
            this.dtpPurchaseDate.Name = "dtpPurchaseDate";
            this.dtpPurchaseDate.Size = new System.Drawing.Size(200, 21);
            this.dtpPurchaseDate.TabIndex = 21;
            // 
            // label12
            // 
            this.label12.AutoSize = true;
            this.label12.Location = new System.Drawing.Point(350, 80);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(35, 12);
            this.label12.TabIndex = 22;
            this.label12.Text = "版本:";
            // 
            // txtVersion
            // 
            this.txtVersion.Location = new System.Drawing.Point(430, 77);
            this.txtVersion.Name = "txtVersion";
            this.txtVersion.Size = new System.Drawing.Size(200, 21);
            this.txtVersion.TabIndex = 23;
            // 
            // btnAdd
            // 
            this.btnAdd.Location = new System.Drawing.Point(350, 300);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(75, 23);
            this.btnAdd.TabIndex = 24;
            this.btnAdd.Text = "添加";
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(450, 300);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 25;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // BookAddForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(684, 361);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnAdd);
            this.Controls.Add(this.txtVersion);
            this.Controls.Add(this.label12);
            this.Controls.Add(this.dtpPurchaseDate);
            this.Controls.Add(this.label11);
            this.Controls.Add(this.cboShelf);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.txtDescription);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.txtPages);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.txtPrice);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.txtCategory);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.dtpPublishDate);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.cboPublisher);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.txtAuthor);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txtBookName);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txtISBN);
            this.Controls.Add(this.label1);
            this.Name = "BookAddForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "添加图书";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private Label label1;
        private TextBox txtISBN;
        private Label label2;
        private TextBox txtBookName;
        private Label label3;
        private TextBox txtAuthor;
        private Label label4;
        private ComboBox cboPublisher;
        private Label label5;
        private DateTimePicker dtpPublishDate;
        private Label label6;
        private TextBox txtCategory;
        private Label label7;
        private TextBox txtPrice;
        private Label label8;
        private TextBox txtPages;
        private Label label9;
        private TextBox txtDescription;
        private Label label10;
        private ComboBox cboShelf;
        private Label label11;
        private DateTimePicker dtpPurchaseDate;
        private Label label12;
        private TextBox txtVersion;
        private Button btnAdd;
        private Button btnCancel;

        private void LoadPublishers()
        {
            try
            {
                using (var connection = Connection.GetMySqlConnection())
                {
                    connection.Open();
                    string sql = "SELECT publisher_id, publisher_name FROM publisher_info ORDER BY publisher_name";
                    using (var command = new MySqlCommand(sql, connection))
                    {
                        using (var reader = command.ExecuteReader())
                        {
                            DataTable dt = new DataTable();
                            dt.Columns.Add("publisher_id", typeof(int));
                            dt.Columns.Add("publisher_name", typeof(string));
                            dt.Rows.Add(0, "--请选择出版社--");

                            while (reader.Read())
                            {
                                dt.Rows.Add(reader.GetInt32("publisher_id"), reader.GetString("publisher_name"));
                            }

                            cboPublisher.DataSource = dt;
                            cboPublisher.DisplayMember = "publisher_name";
                            cboPublisher.ValueMember = "publisher_id";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载出版社信息失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LoadShelves()
        {
            try
            {
                using (var connection = Connection.GetMySqlConnection())
                {
                    connection.Open();
                    string sql = "SELECT shelf_id, shelf_code FROM shelf_info ORDER BY shelf_code";
                    using (var command = new MySqlCommand(sql, connection))
                    {
                        using (var reader = command.ExecuteReader())
                        {
                            DataTable dt = new DataTable();
                            dt.Columns.Add("shelf_id", typeof(int));
                            dt.Columns.Add("shelf_code", typeof(string));
                            dt.Rows.Add(0, "--请选择书架--");

                            while (reader.Read())
                            {
                                dt.Rows.Add(reader.GetInt32("shelf_id"), reader.GetString("shelf_code"));
                            }

                            cboShelf.DataSource = dt;
                            cboShelf.DisplayMember = "shelf_code";
                            cboShelf.ValueMember = "shelf_id";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载书架信息失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // 验证输入
            if (string.IsNullOrWhiteSpace(txtISBN.Text))
            {
                MessageBox.Show("请输入ISBN！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtISBN.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(txtBookName.Text))
            {
                MessageBox.Show("请输入图书名称！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtBookName.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(txtAuthor.Text))
            {
                MessageBox.Show("请输入作者！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtAuthor.Focus();
                return;
            }

            if (cboPublisher.SelectedValue == null || (int)cboPublisher.SelectedValue <= 0)
            {
                MessageBox.Show("请选择出版社！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cboPublisher.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(txtCategory.Text))
            {
                MessageBox.Show("请输入图书分类！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCategory.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(txtPrice.Text))
            {
                MessageBox.Show("请输入价格！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPrice.Focus();
                return;
            }

            decimal price;
            if (!decimal.TryParse(txtPrice.Text, out price) || price <= 0)
            {
                MessageBox.Show("请输入有效的价格！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPrice.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(txtPages.Text))
            {
                MessageBox.Show("请输入页数！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPages.Focus();
                return;
            }

            int pages;
            if (!int.TryParse(txtPages.Text, out pages) || pages <= 0)
            {
                MessageBox.Show("请输入有效的页数！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPages.Focus();
                return;
            }

            if (cboShelf.SelectedValue == null || (int)cboShelf.SelectedValue <= 0)
            {
                MessageBox.Show("请选择书架！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cboShelf.Focus();
                return;
            }

            try
            {
                using (var connection = Connection.GetMySqlConnection())
                {
                    connection.Open();
                    // 检查ISBN是否已存在
                    string checkSql = "SELECT COUNT(*) FROM book_info WHERE isbn = @isbn";
                    using (var checkCommand = new MySqlCommand(checkSql, connection))
                    {
                        checkCommand.Parameters.AddWithValue("@isbn", txtISBN.Text.Trim());
                        int count = Convert.ToInt32(checkCommand.ExecuteScalar());
                        if (count > 0)
                        {
                            MessageBox.Show("该ISBN已存在，请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            txtISBN.Focus();
                            return;
                        }
                    }

                    // 插入新图书
                    string insertSql = @"INSERT INTO book_info 
                        (isbn, book_name, author, publisher_id, publish_date, book_category, 
                         book_price, book_pages, book_desc, book_status, shelf_id, purchase_date, book_version) 
                        VALUES 
                        (@isbn, @book_name, @author, @publisher_id, @publish_date, @book_category, 
                         @book_price, @book_pages, @book_desc, 1, @shelf_id, @purchase_date, @book_version)";

                    using (var insertCommand = new MySqlCommand(insertSql, connection))
                    {
                        insertCommand.Parameters.AddWithValue("@isbn", txtISBN.Text.Trim());
                        insertCommand.Parameters.AddWithValue("@book_name", txtBookName.Text.Trim());
                        insertCommand.Parameters.AddWithValue("@author", txtAuthor.Text.Trim());
                        insertCommand.Parameters.AddWithValue("@publisher_id", cboPublisher.SelectedValue);
                        insertCommand.Parameters.AddWithValue("@publish_date", dtpPublishDate.Value.Date);
                        insertCommand.Parameters.AddWithValue("@book_category", txtCategory.Text.Trim());
                        insertCommand.Parameters.AddWithValue("@book_price", price);
                        insertCommand.Parameters.AddWithValue("@book_pages", pages);
                        insertCommand.Parameters.AddWithValue("@book_desc", string.IsNullOrWhiteSpace(txtDescription.Text) ? (object)DBNull.Value : txtDescription.Text.Trim());
                        insertCommand.Parameters.AddWithValue("@shelf_id", cboShelf.SelectedValue);
                        insertCommand.Parameters.AddWithValue("@purchase_date", dtpPurchaseDate.Value.Date);
                        insertCommand.Parameters.AddWithValue("@book_version", string.IsNullOrWhiteSpace(txtVersion.Text) ? (object)DBNull.Value : txtVersion.Text.Trim());

                        int result = insertCommand.ExecuteNonQuery();
                        if (result > 0)
                        {
                            MessageBox.Show("图书添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("图书添加失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"添加图书失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
